package com.example.bugshaker.email;

import com.example.mybugshaker.flow.email.App;
import com.example.mybugshaker.flow.email.InstallSource;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import ohos.bundle.ApplicationInfo;
import ohos.bundle.BundleInfo;
import ohos.bundle.IBundleManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.RemoteException;
import org.junit.Test;

import static org.junit.Assert.*;

public class AppTest {

    public static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TestApp");

    public static final Ability currentTopAbility = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
    public static final Context mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();

    final IBundleManager iBundleManager = mContext.getBundleManager();
    final ApplicationInfo applicationInfo = mContext.getApplicationInfo();
    final BundleInfo bundleInfo = getPackageInfo();

    String name;
    String versionName;
    int versionCode;
    InstallSource installSource;

    App app = new App(mContext);

    @Test
    public void getName() {
        name = mContext.getApplicationInfo().getLabel();
        assertEquals(name, app.getName());
    }

    @Test
    public void getVersionName() {
        assert bundleInfo != null;
        versionName = bundleInfo.getName();
        assertEquals(versionName, app.getVersionName());
    }

    @Test
    public void getVersionCode() {
        assert bundleInfo != null;
        versionCode = bundleInfo.getUid();
        assertEquals(versionCode, app.getVersionCode());
    }

    private BundleInfo getPackageInfo() {
        final IBundleManager iBundleManager = AppTest.mContext.getBundleManager();
        try {
            return iBundleManager.getBundleInfo(AppTest.mContext.getBundleName(),0);
        } catch (RemoteException e) {
            //noinspection ConstantConditions: packageInfo should always be available for the embedding app.
            e.printStackTrace();
            return null;
        }
    }
}